package com.example.presentation_libary;

import android.content.Context;
import android.content.Intent;

import com.example.presentation_libary.eventBus.MsgEvent;
import com.example.presentation_libary.interfaces.ScreenAddViewInterFace;
import com.example.presentation_libary.interfaces.ScreenStartServiceInterFace;
import com.example.presentation_libary.presentation.MultiScreenPresentation;
import com.example.presentation_libary.service.MultiScreenstartService;


/**
 * @author: tjf
 * @date: 2022-10-20
 * @desc 启动服务 副屏的添加及使用和释放
 */
public class ScreenStartServiceUtils {
    public static String TAG = "辅屏服务工具类";

    private MultiScreenPresentation presentation;

    public ScreenStartServiceUtils() {

    }

    public static class ScreenServiceUtilsHolder {
        public static final ScreenStartServiceUtils instance = new ScreenStartServiceUtils();

    }

    public static ScreenStartServiceUtils getInstance() {
        return ScreenServiceUtilsHolder.instance;
    }


    //
    public void setCallbackInface(MultiScreenPresentation mPresentation) {
        presentation = mPresentation;
        if (serviceInterFace != null) {
            serviceInterFace.auxiliaryScreenStartsAndDisplaysSuccess();
        }
    }

    /**
     * 启动双屏异显的服务
     * 进行演示辅屏
     */
    public void startService(Context context) {
        Intent intent = new Intent(context, MultiScreenstartService.class);
        context.startService(intent);
    }

    //添加显示的副屏内容及类型
    public void addAuxiliaryScreenTypeAndContent(String ScreenType, String json) {
         if (presentation != null) {
            presentation.onMsgPresentationBus(new MsgEvent(ScreenType, json));
        }
    }

    // 设置副屏类型 和数据的处理回调   这个必须是在服务启动后再其回调中使用
    public void setCallbackSecondaryScreenTypeAndContent(ScreenAddViewInterFace screenAddViewInterFace) {
        if (presentation != null) {
            presentation.setScreenAddViewInterFace(screenAddViewInterFace);
        }
    }


    //停止服务 进行释放
    public void close(Context context) {
        context.stopService(new Intent(context, MultiScreenstartService.class));
        setServiceInterFace(null);
    }

    //接口对象
    private ScreenStartServiceInterFace serviceInterFace;

    public void setServiceInterFace(ScreenStartServiceInterFace serviceInterFace) {
        this.serviceInterFace = serviceInterFace;
    }
}
